package com.cncn.youmengsharelib;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeConfig;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.SmsHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * 分享的辅助类
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-04-07
 * @Version: 1.0.0
 */
public class ShareHelper {

    //region 各个平台的分享api
    public static void share(Activity activity, SharePlatform plat, ShareData shareInfo, ShareListener shareListener) {
//        initShareSDK(activity);

        switch (plat) {
            case QQ:
                shareQQ(activity, shareInfo, shareListener);
                break;

            case QZONE:
                shareQzone(activity, shareInfo, shareListener);
                break;

            case SINAWEIBO:
                //设置新浪SSO handler
                shareSinaWeibo(activity, shareInfo, shareListener);
                break;

            case WECHAT:
                shareWechat(activity, shareInfo, shareListener);
                break;

            case WECHATMOMENTS:
                shareWechatMoments(activity, shareInfo, shareListener);
                break;

            case SMS:
                shareSMS(activity, shareInfo, shareListener);
                break;
        }
    }

    public static void shareSMS(Activity activity, ShareData shareInfo, ShareListener shareListener) {
        UMSocialService umSocialService = getUMSocialService(activity, shareInfo);
        SmsHandler smsHandler = new SmsHandler();
        smsHandler.addToSocialSDK();
        umSocialService.postShare(activity, SHARE_MEDIA.SMS,
                translateListener(activity, shareListener));
    }


    // 微信朋友圈
    public static void shareWechatMoments(Activity activity, ShareData info, ShareListener shareListener) {
        initShareSDK(activity);
        UMSocialService umSocialService = getUMSocialService(activity, info);
        //设置微信朋友圈分享内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent(info.getContent());
        //设置朋友圈title
        circleMedia.setTitle(info.getTitle());
        circleMedia.setShareImage(getUMImage(activity, info));
//        String url = info.getTargetUrl() + "&is_weixin=1";
        String url = info.getTargetUrl();
        circleMedia.setTargetUrl(url);
        umSocialService.setShareMedia(circleMedia);
        umSocialService.postShare(activity, SHARE_MEDIA.WEIXIN_CIRCLE,
                translateListener(activity, shareListener));
    }


    public static void shareSinaWeibo(Activity activity, ShareData shareInfo, ShareListener shareListener) {
        initShareSDK(activity);
        UMSocialService umSocialService = getUMSocialService(activity, shareInfo);
        SinaSsoHandler sinaSsoHandler = new SinaSsoHandler();
        umSocialService.getConfig().setSsoHandler(sinaSsoHandler);
        umSocialService.getConfig().setSinaCallbackUrl(BuildConfig.SINA_REDIRECT_URL);
        umSocialService.postShare(activity, SHARE_MEDIA.SINA,
                translateListener(activity, shareListener));
    }

    public static void shareQzone(Activity activity, ShareData shareInfo, ShareListener shareListener) {
        initShareSDK(activity);
        UMSocialService umSocialService = getUMSocialService(activity, shareInfo);
        //设置微信好友分享内容
        QZoneShareContent qzoneContent = new QZoneShareContent();
        //设置分享文字
        qzoneContent.setShareContent(shareInfo.getContent());
        //设置title
        qzoneContent.setTitle(shareInfo.getTitle());
        //设置分享内容跳转URL
        qzoneContent.setTargetUrl(shareInfo.getTargetUrl());
        //设置分享图片
        qzoneContent.setShareImage(getUMImage(activity, shareInfo));
        umSocialService.setShareMedia(qzoneContent);
        umSocialService.postShare(activity, SHARE_MEDIA.QZONE,
                translateListener(activity, shareListener));
    }

    // 微信好友
    public static void shareWechat(Activity activity, ShareData info, ShareListener shareListener) {
        initShareSDK(activity);
        UMSocialService umSocialService = getUMSocialService(activity, info);
        //设置微信好友分享内容
        WeiXinShareContent weixinContent = new WeiXinShareContent();
        //设置分享文字
        weixinContent.setShareContent(info.getContent());
        //设置title
        weixinContent.setTitle(info.getTitle());
        //设置分享内容跳转URL
        weixinContent.setTargetUrl(info.getTargetUrl());
        //设置分享图片
        weixinContent.setShareImage(getUMImage(activity, info));
        umSocialService.setShareMedia(weixinContent);
        umSocialService.postShare(activity, SHARE_MEDIA.WEIXIN,
                translateListener(activity, shareListener));
    }


    public static void shareQQ(Activity activity, ShareData shareInfo, ShareListener shareListener) {
        initShareSDK(activity);
        UMSocialService service = getUMSocialService(activity, shareInfo);
        QQShareContent qqShareContent = new QQShareContent();
        //设置分享内容
        qqShareContent.setShareContent(shareInfo.getContent());
        //设置分享内容跳转URL
        qqShareContent.setTargetUrl(shareInfo.getTargetUrl());
        qqShareContent.setShareImage(getUMImage(activity, shareInfo));
        //设置分享标题
        qqShareContent.setTitle(shareInfo.getTitle());
        service.setShareMedia(qqShareContent);
        service.postShare(activity, SHARE_MEDIA.QQ,
                translateListener(activity, shareListener));
    }

    private static UMImage getUMImage(Context context, ShareData shareInfo) {
        UMImage umImage;
        if (!TextUtils.isEmpty(shareInfo.getImageUrl())) {
            umImage = new UMImage(context, shareInfo.getImageUrl());
        } else if (shareInfo.getImageBitmap() != null) {
            umImage = new UMImage(context, shareInfo.getImageBitmap());
        } else {
            throw new RuntimeException("没有设置分享的图片");
        }
        return umImage;
    }


    /**
     * 转换监听器
     * @param activity
     * @param shareListener
     * @return
     */
    private static SocializeListeners.SnsPostListener translateListener(final Activity activity, final ShareListener shareListener) {
        return new SocializeListeners.SnsPostListener() {

            @Override
            public void onStart() {
                shareListener.onStart();
            }

            @Override
            public void onComplete(SHARE_MEDIA platform, int stCode,
                                   SocializeEntity entity) {
                SharePlatform sharePlatform = translatePlatform(platform);
                if (stCode == 200) {
                    shareListener.onShareSuccess(activity, sharePlatform);

                } else if (stCode == 40000) {
                    shareListener.onShareCancel(activity, sharePlatform);

                } else {
                    shareListener.onShareFailed(activity, sharePlatform);
                }
                // fix bug：重复调用回调
                SocializeConfig.getSocializeConfig().cleanListeners();
            }

        };
    }

    /**
     * 将第三方的平台类转为我们定义的平台类
     * @param platfom
     * @return
     */
    private static SharePlatform translatePlatform(SHARE_MEDIA platfom) {
        if (platfom == SHARE_MEDIA.SMS) {
            return SharePlatform.SMS;

        } else if (platfom == SHARE_MEDIA.SINA) {
            return SharePlatform.SINAWEIBO;

        } else if (platfom == SHARE_MEDIA.WEIXIN) {
            return SharePlatform.WECHAT;

        } else if (platfom == SHARE_MEDIA.WEIXIN_CIRCLE) {
            return SharePlatform.WECHATMOMENTS;

        } else if (platfom == SHARE_MEDIA.QQ) {
            return SharePlatform.QQ;

        } else if (platfom == SHARE_MEDIA.QZONE) {
            return SharePlatform.QZONE;
        } else {
            throw new RuntimeException("没有找到对应的分享平台");
        }
    }

    //endregion


    //region 初始化或者配置工作
    public static void initShareSDK(Activity context) {
        // 添加QQ平台
        UMQQSsoHandler qqHandler = new UMQQSsoHandler(context,
                BuildConfig.QQ_APP_ID, BuildConfig.QQ_APP_SECRET);
        qqHandler.addToSocialSDK();
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(context, BuildConfig.QQ_APP_ID, BuildConfig.QQ_APP_SECRET);
        qZoneSsoHandler.addToSocialSDK();

        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(context, BuildConfig.WX_APP_ID, BuildConfig.WX_APP_SECRET);
        wxHandler.addToSocialSDK();

        // 支持微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(context, BuildConfig.WX_APP_ID, BuildConfig.WX_APP_SECRET);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();


    }


    private static UMSocialService getUMSocialService(Context context, ShareData shareInfo) {
        UMSocialService umService = UMServiceFactory
                .getUMSocialService("myshare");
        UMImage umImage = getUMImage(context, shareInfo);
        umImage.setTargetUrl(shareInfo.getTargetUrl());
        umImage.setTitle(shareInfo.getTitle());

        umService.setShareContent(shareInfo.getContent());
        umService.setShareMedia(umImage);
        umService.getConfig().closeToast();
        return umService;
    }
    //endregion

}


